package com.sbl.bajw.pc.action;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sbl.bajw.common.CoordConver;
import com.sbl.bajw.common.ExcelUtil;
import com.sbl.bajw.common.web.JsonResultUtil;
import com.sbl.bajw.common.web.MessageException;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.common.web.SessionUnit;
import com.sbl.bajw.pc.model.UserModel;
import com.sbl.bajw.pc.service.AppVersionTModelService;
import com.sbl.bajw.pc.service.UserService;
import com.sbl.bajw.util.ExecptionUtil;
import com.sbl.bajw.util.JsonUtil;
import com.sbl.bajw.util.LogUtil;

@Controller
@RequestMapping("/User")
public class UserAction {
	
	@Resource
	private UserService service;
	
	@Resource
	private AppVersionTModelService appService;
	
	static Logger log = null;
	static Logger gpsLog = null;
	static Logger gpsErrorLog = null;

	static{
		log = LogUtil.getLogger("user_log");
		gpsLog = LogUtil.getLogger("gpsLog");
		gpsErrorLog = LogUtil.getLogger("gpsErrorLog");
	}
	
	/**
	 * 用户登录
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @return
	 * @throws MessageException
	 */
	@RequestMapping(value = "/Login")
	public @ResponseBody String login(HttpServletRequest request, HttpServletResponse response) throws MessageException{
		
		String username = RequestUnit.getString("username");
		String password = RequestUnit.getString("password");
		String version = RequestUnit.getString("version");
		String os = RequestUnit.getString("os");
		
		if(username==null || username.equals("") || password==null || password.equals("")){
			throw new MessageException("用户名密码不能为空","4");
		}
		
		if(os != null && !"ios".equals(os) && !"pc".equals(os)){
			if(version != null){
				if(!"V0.0.0.0".equals(version)){
					//限制APP用户非最新版本登录
					HashMap<String, Object> app = appService.getVersion("bajw");
					if(app != null && !"".equals(app)){
						if(version.compareTo(String.valueOf(app.get("APP_VERSITON"))) < 0){
							throw new MessageException("当前版本不可用，请重新打开APP并按照提示更新！", "3");
						}
					}
				}
			}else{
				throw new MessageException("当前版本不可用，请重新打开APP并按照提示更新！", "3");
			}
		}
		
		UserModel model = service.getModelByName(username);
		
		if(model == null || "0".equals(model.getStatus())){
			throw new MessageException("账号不存在","4");
		}
		
		if(!model.getPassword().equals(password)){
			throw new MessageException("密码错误","4");
		}
		
		//用户APP登录版本入库
		UserModel user = new UserModel();
		user.setId(model.getId());
		user.setUpdatedBy(model.getId());
		user.setAppVersion(version);
		service.update(user);
		
		SessionUnit.setUserId(model.getId()+"");
		SessionUnit.setUserName(model.getShowname());
		SessionUnit.setDeptId(model.getDeptid());
		
		log.info("App登录：userid:"+model.getId()+"username:"+username+",password:"+password+",version:"+version);
		HashMap<String, String> result = new HashMap<String, String>();
		result.put("msg", "登录成功");
		result.put("sessionid", request.getSession().getId());
		result.put("headImage", model.getImageurl());
		result.put("userid", model.getId());
		result.put("success", "true");
		result.put("username", model.getUsername());
		result.put("showname", model.getShowname());
		result.put("code", "1");
		String resultStr = JsonUtil.ConvertObjectToJson(result);
		return resultStr;
	}
	
	/**
	 * 注册新用户
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @return
	 * @throws MessageException
	 */
	@RequestMapping(value = "/register")
	public @ResponseBody String register(HttpServletRequest request, HttpServletResponse response) throws MessageException{
		UserModel user = RequestUnit.getModel(UserModel.class);
		checkModel(user);
		String pcs = RequestUnit.getString("pcs");
		String jq = RequestUnit.getString("jq");
		String zrq = RequestUnit.getString("zrq");
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String deptfullid = "";
			if(pcs != null && !pcs.equals("")){
				if(jq != null && !jq.equals("")){
					if(zrq != null && !zrq.equals("")){
						deptfullid = "0-1-" + pcs + "-" + jq + "-" + zrq + "-";
					}else{
						deptfullid = "0-1-" + pcs + "-" + jq + "-";
					}
				}else{
					deptfullid = "0-1-" + pcs + "-";
				}	
			}else{
				deptfullid = "0-1-";
			}
			log.info("用户注册 ==>登录账户："+user.getUsername()+",密码："+user.getPassword()+",姓名："+user.getShowname()
					+",性别："+user.getSex()+",手机号码："+user.getTelNumber()+",身份："+user.getUsertype()+",职务："+user.getUserJob()
					+",工作证号："+user.getMjjh()+",单位："+deptfullid);
			user.setDeptfullid(deptfullid);
			user.setStatus("1");
			service.register(user);
			map.put("code", "1");
			map.put("msg", "注册成功");
			map.put("success", "true");
		} catch (Exception e) {
			String msg = ExecptionUtil.getStackTrace(e);
			e.printStackTrace();
			log.info(msg);
			throw new MessageException(e.getMessage(), "3");
		}
		return JsonUtil.ConvertObjectToJson(map);
	}
	
	/**
	 * 删除用户
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/deleteUsers")
	public @ResponseBody String deleteUsers(HttpServletRequest request, HttpServletResponse response) throws Exception{
		HashMap<String, String> parm = RequestUnit.getHashMapParmS();
		service.deleteUser(parm.get("ids").split(","), parm.get("loginnames").split(","));
		return JsonResultUtil.Success();
	}
	
	@RequestMapping(value = "/updateUsersLocaltion")
	public @ResponseBody String updateUsersLocaltion(HttpServletRequest request, HttpServletResponse response) throws Exception{
		HashMap<String, String> parm = RequestUnit.getHashMapParmS();
		service.updateUsersLocation(parm.get("loginnames").split(","), parm.get("timer"));
		return JsonResultUtil.Success();
	}
	
	@RequestMapping(value = "/getUserGpsListApp")
	public @ResponseBody String getUserGpsListApp(HttpServletRequest request, HttpServletResponse response){
		log.info("用户位置查询");
		List<HashMap<String, Object>> list = service.getUserGpsList();
		log.info("用户位置：" + list);
		String json = JsonResultUtil.Success("", JsonUtil.ConvertArrayToJson(list));
		return json;
	}
	
	@RequestMapping(value = "/updateMsgPushInfo")
	public @ResponseBody String updateMsgPushInfo(HttpServletRequest request, HttpServletResponse response) throws MessageException{
		String userId = RequestUnit.getString("user_id");
		String baiduUserid = RequestUnit.getString("baidu_userid");
		String baiduChannelid = RequestUnit.getString("baidu_channelid");
		UserModel user = new UserModel();
		user.setId(userId);
		user.setBaiduUserid(baiduUserid);
		user.setBaiduChannelid(baiduChannelid);
		String json = null;
		try {
			service.update(user);
			json = JsonResultUtil.Success();
		} catch (Exception e) {
			log.info(e);
			e.printStackTrace();
			throw new MessageException("失败", "3");
		}
		return json;
	}
	
	@RequestMapping(value = "/Exsit")
	public @ResponseBody String Exsit(HttpServletRequest request, HttpServletResponse response){
		SessionUnit.setUserId(null);
		Map<String, String> map = new HashMap<String, String>();
		map.put("msg", "退出成功！");
		return JsonUtil.ConvertObjectToJson(map);
	}
	
	@RequestMapping(value = "/check")
	public @ResponseBody String check(HttpServletRequest request, HttpServletResponse response){
		String result = SessionUnit.getUserName() +","+ SessionUnit.getUserId();
		return result;
	}
	
	@RequestMapping(value = "/getUserInfo")
	public @ResponseBody String getUserInfo(HttpServletRequest request, HttpServletResponse response){
		String id = RequestUnit.getString("id");
		UserModel model = service.getModel(id);
		double[] xyConvered = CoordConver.converWgs84ToDb09(model.getGpsX(),model.getGpsY());
		model.setGpsX(xyConvered[0]);
		model.setGpsY(xyConvered[1]);
		model.setPassword("");
		String jsonResult = JsonUtil.ConvertObjectToJson(model);
		return jsonResult;
	}
	
	@RequestMapping(value = "/getModelWithGpsInfo")
	public @ResponseBody String getModelWithGpsInfo(HttpServletRequest request, HttpServletResponse response){
		String id = RequestUnit.getString("id");
		UserModel model = service.getModelWithGpsInfo(id);
		double[] xyConvered = CoordConver.converWgs84ToDb09(model.getGpsX(),model.getGpsY());
		model.setGpsX(xyConvered[0]);
		model.setGpsY(xyConvered[1]);
		model.setPassword("");
		String jsonResult = JsonUtil.ConvertObjectToJson(model);
		return jsonResult;
	}
	
	@RequestMapping(value = "/postLocation")
	public @ResponseBody String postLocation(HttpServletRequest request, HttpServletResponse response){
		String userId = request.getParameter("user_id");
		gpsLog.info("userid:"+userId);
		String json = "";
		return json;
	}
	
	@RequestMapping(value="/getHistoryPoition")
	public @ResponseBody String getHistoryPoition(@RequestParam(value="no",required=false) String userid,
			@RequestParam(value="sTime",required=false) String sTime,
			@RequestParam(value="eTime",required=false) String eTime) throws Exception{
		UserModel model = service.getModel(userid);
		HashMap<String, Object> parm = new HashMap<String, Object>();
		parm.put("username", model.getUsername());
		try {
			parm.put("sTime", sTime);
			parm.put("eTime", eTime);
		} catch (Exception e) {
			throw new MessageException("时间格式不正确。");
		}
		
		List<HashMap<String, Object>> models = service.getHistoryPoition(parm);
		String result = JsonUtil.ConvertArrayToJson(models);
		return result;
	} 
	
	/**
	 * 数据权限管理列表查询
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getLocalhostModelList")
	public @ResponseBody String getLocalhostModelList(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		String json = service.getLocalhostModelList(parm);
		return json;
	}
	
	/**
	 * 用户列表简单跳转
	 * @作者：罗琦
	 * @return
	 */
	@RequestMapping(value = "/easyList")
	public ModelAndView easyList(){
		ModelAndView mav = new ModelAndView("/view/manager/user/UserEasyList");
		return mav;
	}
	
	@RequestMapping(value = "/complexList")
	public ModelAndView complexList(){
		ModelAndView mav = new ModelAndView("/view/manager/user/UserList");
		return mav;
	}
	
	/**
	 * 简化的用户列表展示
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getUserEasyList")
	public @ResponseBody String getUserEasyList(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		String json = service.getUserEasyList(parm);
		return json;
	}
	
	/**
	 * 用户列表展示
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getUserList")
	public @ResponseBody String getUserList(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		log.info("条件==>账号："+parm.get("username")+"，用户名："+parm.get("showname")+"，电话号码："+parm.get("telNumber")+"，在线状态："
				+parm.get("onliner")+"，用户类型："+parm.get("usertype")+"，职务："+parm.get("userJob")+"，注册时间："+parm.get("start_date")+"~"+parm.get("stop_date"));
		String json = service.getUserList(parm);
		return json;
	}
	
	@RequestMapping(value = "/getModelsForMap")
	public @ResponseBody String getModelsForMap(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, String> parm = RequestUnit.getHashMapParmS();
		List<HashMap<String, String>> list = service.getModelsForMap(parm);
		String json = JsonUtil.ConvertArrayToJson(list);
		return JsonResultUtil.Success(json);
	}
	
	/**
	 * 用户列表导出
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/downloadUserList")
	public void downloadUserList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		List<HashMap<String, Object>> list = service.getModelList(parm);
		LinkedHashMap<String, String> column = new LinkedHashMap<String, String>();
		column.put("ID", "序号");
		column.put("USERNAME", "账号");
		column.put("SHOWNAME", "用户名");
		column.put("TEL_NUMBER", "电话号码");
		column.put("STATUS", "用户状态");
		column.put("ONLINER", "在线状态");
		column.put("USERTYPE", "用户类型");
		column.put("USER_JOB", "职位");
		column.put("PCSNAME", "派出所");
		column.put("JQNAME", "警区");
		column.put("ZRQNAME", "责任区");
		column.put("IMEI", "IMEI");
		HSSFWorkbook wb = ExcelUtil.createExcel(list, column);
		OutputStream outStream = response.getOutputStream();
		response.setContentType("application/x-msdownload");  
		response.setHeader("Content-Disposition","attachment;filename=user.xls");  
		wb.write(outStream);
	}
	
	/**
	 * 获取人员统计，并展示
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getCount")
	public @ResponseBody String getCount(HttpServletRequest request, HttpServletResponse response){
		String json = JsonUtil.ConvertObjectToJson(service.getCount());
		return json;
	}
	
	/**
	 * 更新头像
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/updateImageUrl")
	public @ResponseBody String updateImageUrl(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String id = RequestUnit.getString("user_id");
		UserModel model = new UserModel();
		String json = "";
		String path = "";
		try {
			model.setId(id);
			log.info(id + "上传头像");
			path = service.updateImageUrl(model);
			json = JsonResultUtil.Success("", "\""+path+"\"");
		} catch (Exception e) {
			log.error("头像上传出错", e);
			throw new MessageException("系统异常", "4");
		}
		return json;
	}
	
	/**
	 * 修改密码
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @return
	 * @throws MessageException
	 */
	@RequestMapping(value = "/changePwd")
	public @ResponseBody String changePwd(HttpServletRequest request, HttpServletResponse response) throws MessageException{
		String id = RequestUnit.getString("user_id");
		String oldPwd = RequestUnit.getString("oldPwd");
		String newPwd = RequestUnit.getString("newPwd");
		Map<String, String> map = new HashMap<String, String>();
		try {
			UserModel user = service.getModel(id);
			log.info(user.getShowname() + "修改密码，原密码：" + oldPwd + ",新密码：" + newPwd);
			if(oldPwd.equals(user.getPassword())){
				service.update(id, newPwd);
				map.put("code", "1");
				map.put("success", "true");
				map.put("msg", "修改成功");
				log.info(user.getShowname() + "修改密码成功！");
			}else{
				log.info(user.getShowname() + "原密码不正确！");
				map.put("code", "2");
				map.put("success", "false");
				map.put("msg", "原密码不正确");
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.info(e);
			throw new MessageException("修改密码失败", "3");
		}
		return JsonUtil.ConvertObjectToJson(map);
	}
	
	@RequestMapping(value = "/identityCheck")
	public @ResponseBody String searchPwd(HttpServletRequest request, HttpServletResponse response){
		UserModel user = RequestUnit.getModel(UserModel.class);
		HashMap<String, String> parm = new HashMap<String, String>();
		parm.put("username", user.getUsername());
		parm.put("showname", user.getShowname());
		parm.put("telNumber", user.getTelNumber());
		int num = service.getUserCount(parm);
		Map<String, String> map = new HashMap<String, String>();
		if(num == 1){
			map.put("success", "true");
			map.put("msg", "成功");
		}else{
			map.put("success", "false");
			map.put("msg", "身份校验失败！");
		}
		map.put("data", user.getUsername());
		return JsonUtil.ConvertObjectToJson(map);
	}
	
	@RequestMapping(value = "/updatePwd")
	public @ResponseBody String updatePwd(HttpServletRequest request, HttpServletResponse response) throws MessageException{
		String username = RequestUnit.getString("loginName");
		String pwd = RequestUnit.getString("loginPwd");
		HashMap<String, String> parm = new HashMap<String, String>();
		HashMap<String, String> map = new HashMap<String, String>();
		parm.put("username", username);
		parm.put("pwd", pwd);
		try {
			log.info("忘记密码修改-->账号：" + username + ",新密码：" + pwd);
			service.updatePwd(parm);
			map.put("code", "1");
			map.put("msg", "密码修改成功！");
			map.put("success", "true");
		} catch (Exception e) {
			e.printStackTrace();
			log.info(e);
			throw new MessageException("修改失败！", "3");
		}
		return JsonUtil.ConvertObjectToJson(map);
	}
	
	
	@RequestMapping(value = "/submitUser")
	public @ResponseBody String submitUser(HttpServletRequest request, HttpServletResponse response) throws MessageException{
		String id = RequestUnit.getString("user_id");
		String sex = RequestUnit.getString("sex");
		String username = RequestUnit.getString("showname");
		String phone = RequestUnit.getString("telNumber");
		String type = RequestUnit.getString("usertype");
		String job = RequestUnit.getString("zw");
		String mjjh = RequestUnit.getString("mjjh");
		String pcs = RequestUnit.getString("pcs");
		String jq = RequestUnit.getString("jq");
		String zrq = RequestUnit.getString("zrq");
		String cardId = RequestUnit.getString("cardId");
		String bankcardNo = RequestUnit.getString("bankcardNo");
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String deptfullid = "";
			if(pcs != null && !pcs.equals("")){
				if(jq != null && !jq.equals("")){
					if(zrq != null && !zrq.equals("")){
						deptfullid = "0-1-" + pcs + "-" + jq + "-" + zrq + "-";
					}else{
						deptfullid = "0-1-" + pcs + "-" + jq + "-";
					}
				}else{
					deptfullid = "0-1-" + pcs + "-";
				}	
			}else{
				deptfullid = "0-1-";
			}
			log.info("用户信息修改 ==>用户id："+id+",姓名："+username+",性别："+sex+",手机号码："+phone+",身份："+type+",职务："+job+",工作证号："+mjjh+",单位："+deptfullid);
			service.updateUser(id, sex, username, phone, type, job, mjjh, deptfullid,cardId,bankcardNo);
			map.put("code", "1");
			map.put("msg", "个人信息修改成功");
			map.put("success", "true");
		}catch (Exception e) {
			e.printStackTrace();
			log.info(e);
			throw new MessageException("个人信息修改失败", "3");
		}
		return JsonUtil.ConvertObjectToJson(map);
	}
	
	@RequestMapping(value = "/updateApp")
	public @ResponseBody String updateApp(HttpServletRequest request, HttpServletResponse response) throws MessageException{
		UserModel user = RequestUnit.getModel(UserModel.class);
		String pcs = RequestUnit.getString("pcs");
		String jq = RequestUnit.getString("jq");
		String zrq = RequestUnit.getString("zrq");
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String deptfullid = "";
			if(pcs != null && !pcs.equals("")){
				if(jq != null && !jq.equals("")){
					if(zrq != null && !zrq.equals("")){
						deptfullid = "0-1-" + pcs + "-" + jq + "-" + zrq + "-";
					}else{
						deptfullid = "0-1-" + pcs + "-" + jq + "-";
					}
				}else{
					deptfullid = "0-1-" + pcs + "-";
				}	
			}else{
				deptfullid = "0-1-";
			}
			user.setDeptfullid(deptfullid);
			service.updateApp(user);
			map.put("code", "1");
			map.put("msg", "修改成功");
			map.put("success", "true");
		} catch (Exception e) {
			String msg = ExecptionUtil.getStackTrace(e);
			e.printStackTrace();
			log.info(user.toString()+",异常信息："+msg);
			throw new MessageException(e.getMessage(), "3");
		}
		return JsonUtil.ConvertObjectToJson(map);
	}
	
	@RequestMapping(value = "/importUser")
	public @ResponseBody String importUser(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String file = RequestUnit.getString("fileName");
		InputStream is = new FileInputStream(file);
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is); 
		UserModel user = null;
		String json = "";
		List<UserModel> list = new ArrayList<UserModel>();
		for(int i = 0; i < hssfWorkbook.getNumberOfSheets(); i++){
			HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(i);
			if(hssfSheet == null){
				continue;
			}
			// 循环行Row  
            for (int j = 0; j <= hssfSheet.getLastRowNum(); j++) {  
                HSSFRow hssfRow = hssfSheet.getRow(j);  
                if (hssfRow == null) {  
                    continue;  
                }
                user = new UserModel();
                HSSFCell id = hssfRow.getCell(0);
                if(id == null){
                	continue;
                }
                user.setId(getValue(id));
                HSSFCell username = hssfRow.getCell(1);
                if(username == null){
                	continue;
                }
                user.setUsername(getValue(username));
                HSSFCell showname = hssfRow.getCell(2);
                if(showname == null){
                	continue;
                }
                user.setShowname(getValue(showname));
                HSSFCell telNo = hssfRow.getCell(3);
                if(telNo == null){
                	continue;
                }
                user.setTelNumber(getValue(telNo));
                HSSFCell usertype = hssfRow.getCell(4);
                if(usertype == null){
                	continue;
                }
                user.setUsertype(getValue(usertype));
                list.add(user);
            }
		}
		try {
			service.insert(list);
			json += "{\"success\":\"true\"}";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}
	
	 /**  
     * 得到Excel表中的值  
     *  
     * @param hssfCell  
     *            Excel中的每一个格子  
     * @return Excel中每一个格子中的值  
     */ 
    @SuppressWarnings("static-access")  
    private String getValue(HSSFCell hssfCell) {  
	    if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {  
	        // 返回布尔类型的值  
	        return String.valueOf(hssfCell.getBooleanCellValue());  
	    } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {  
	        // 返回数值类型的值  
	        return String.valueOf((int)hssfCell.getNumericCellValue());  
	    } else {  
	        // 返回字符串类型的值  
	        return String.valueOf(hssfCell.getStringCellValue());  
	    }  
	}  
    
    /**
     * @标题：details
     * @作者：罗琦
     * @时间：2015年6月11日
     * @作用：后台用户详情展示
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/details")
    public ModelAndView details(HttpServletRequest request, HttpServletResponse response){
    	String id = RequestUnit.getString("id");
    	UserModel user = service.getUserModel(id);
    	request.setAttribute("user", user);
    	return new ModelAndView("/view/manager/user/detail");
    }
    
    /**
     * @标题：ExtensionMaterializeList
     * @作者：罗琦
     * @时间：2015年6月11日
     * @作用：后台展示扩展群防群治力量
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/ExtensionMaterializeList")
    public @ResponseBody String ExtensionMaterializeList(HttpServletRequest request, HttpServletResponse response){
    	HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
    	String json = service.getExtensionMaterialize(parm);
    	return JsonUtil.ConvertObjectToJson(json);
    }
    
    @RequestMapping(value = "/useDetailsApp")
    public @ResponseBody String useDetailsApp(HttpServletRequest request, HttpServletResponse response){
    	String id = RequestUnit.getString("user_id");
    	HashMap<String, Object> user = service.getUserModelApp(id);
    	String json = JsonResultUtil.Success("", JsonUtil.ConvertObjectToJson(user));
    	return json;
    }
    
    @RequestMapping(value = "/getUserTeam")
    public @ResponseBody String getUserTeam(HttpServletRequest request, HttpServletResponse response){
    	String id = RequestUnit.getString("user_id");
    	log.info("组员查询");
    	List<HashMap<String, Object>> team = service.getUserTeam(id);
    	log.info("用户" + id + "分组员信息：" + team);
    	String json = JsonResultUtil.Success("", JsonUtil.ConvertArrayToJson(team));
    	return json;
    }
    
    @RequestMapping(value = "/getGroupUsers")
    public @ResponseBody String getGroupUsers(HttpServletRequest request, HttpServletResponse response){
    	String id = RequestUnit.getString("groupid");
    	log.info("组员查询");
    	List<HashMap<String, Object>> team = service.getGroupUsers(id);
    	log.info("用户" + id + "分组员信息：" + team);
    	String json = JsonResultUtil.Success("", JsonUtil.ConvertArrayToJson(team));
    	return json;
    }
    
    @RequestMapping(value = "/getgroups")
    public @ResponseBody String getgroups(HttpServletRequest request, HttpServletResponse response){
    	String id = RequestUnit.getString("user_id");
    	log.info("用户分组查询");
    	List<HashMap<String, Object>> team = service.getgroups(id);
    	log.info("用户" + id + "分组信息：" + team);
    	String json = JsonResultUtil.Success("", JsonUtil.ConvertArrayToJson(team));
    	return json;
    }
    
    /**
     * 获取每个派出所安装APP的统计数量
     * 用于图表展示
     * @作者：罗琦
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getUserAppStatisticsChart")
    public @ResponseBody String getUserAppStatisticsChart(HttpServletRequest request, HttpServletResponse response){
    	List<HashMap<String, String>> list = service.getUserAppStatisticsChart();
    	String json = JsonUtil.ConvertArrayToJson(list);
    	return json;
    }
    
    /**
     * 获取每个派出所安装APP的统计数量
     * 用于列表展示
     * @作者：罗琦
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getUserAppStatisticsGrid")
    public @ResponseBody String getUserAppStatisticsGrid(HttpServletRequest request, HttpServletResponse response){
    	String json = service.getUserAppStatisticsGrid();
    	return json;
    }
    
    public void checkModel(UserModel model) throws MessageException{
    	if(model.getUsername() == null || model.getUsername().trim().equals("")){
    		throw new MessageException("账号不能为空");
    	}
    	
    	if(model.getShowname() == null || model.getUsername().trim().equals("")){
    		throw new MessageException("姓名不能为空");
    	}
    	
    	if(model.getPassword() == null || model.getPassword().trim().equals("")){
    		throw new MessageException("密码不能为空");
    	}
    }
}
